/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ie.tcd.cs.railtickets.server.dao.impl;

import ie.tcd.cs.railtickets.server.dao.TicketDAO;
import ie.tcd.cs.railtickets.server.domain.Ticket;
import java.util.Iterator;
import static ie.tcd.cs.railtickets.server.dao.impl.HibernateSessionFactory.getSession;

/**
 *
 * @author Wenbo Sun
 */
public class TicketDAOImpl extends AbstractBaseDAOImpl implements TicketDAO {

    public Ticket findByNo(String ticketNo) {
        String query = "from Ticket ticket where ticket.ticketNo = ?";
        Ticket ticket = (Ticket) getSession().createQuery(query).setParameter(0, ticketNo).iterate().next();
        return ticket;

    }

    public boolean isExist(String ticketNo) {
        String query = "from Ticket ticket where ticket.ticketNo = ?";
        Iterator it = getSession().createQuery(query).setParameter(0, ticketNo).iterate();
        if (it.hasNext()) {
            return true;
        }
        return false;
    }

    public boolean confirmBy(String ipAddr) {
        String query = "from Ticket ticket where ticket.agent.ipAddr = ?";
        Iterator it = getSession().createQuery(query).setParameter(0, ipAddr).iterate();
        if (it.hasNext()) {
            return true;
        }
        return false;
    }

}
